package klk.shop.objects;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Date;

import klk.shop.utilities.HTMLUtilities;
import klk.shop.utilities.MysqlUtilities;

public class Password extends KLKObject{
	
	protected int customerId;
	protected String hash;
	
	
	public int getCustomerId() {
		return customerId;
	}
	public void setCustomerId(int customerId) {
		this.customerId = customerId;
	}
	
	
	public String getHash() {
		return hash;
	}
	public void setHash(String hash) {
		this.hash = hash;
	}
	
	
	public Password(int customerId, String hash) {
		super();
		this.customerId = customerId;
		this.hash = hash;
	}
	public Password() {
		this(-1, "");
	}
	
	
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder("Password:\n");
		sb.append("customerId: " + customerId + '\n');
		sb.append("hash: " + hash + '\n');		
		return sb.toString();
	}
	
	@Override
	public String toHtml() {
		StringBuilder sb = new StringBuilder( "<table>" );
		sb.append( nl );
		sb.append( HTMLUtilities.TableRow("Password:") );
		sb.append( HTMLUtilities.TableRow("customerId:", customerId) );
		sb.append( HTMLUtilities.TableRow("hash:", hash) );
		sb.append( "</table>" );
		sb.append( nl );
		return sb.toString();
	}

	
	public static Password getPasswordFromDatabase(int customerId) {
		Password password = new Password();
		
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
				
		try	{
			
			conn= MysqlUtilities.getConnection();
			stmt = conn.createStatement();
			
			rs = stmt.executeQuery(
				"SELECT " +
					"hash " + 
				"FROM " +
					"passwords " +
				"WHERE customer_id='"+customerId+"'; "
			);
			
			rs.next();
					
			password.setCustomerId(customerId);
			password.setHash(rs.getString("hash"));
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (rs != null) try { rs.close(); } catch(Exception e) {}
			if (stmt != null) try { stmt.close(); } catch(Exception e) {}
			if (conn != null) try { conn.close(); } catch(Exception e) {}
	    }
		
		return password;	
	}
	
	public static int SetPasswordIntoDatabase(Password password) {
		
		int rowsAffected = 0;

		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
				
		try	{
			
			conn= MysqlUtilities.getConnection();
			stmt = conn.createStatement();
			
			String insertString = 
					"INSERT INTO `passwords` (" +
						"customer_id, hash" +
					") VALUES (" +
							  password.getCustomerId() + ", " +  
						"'" + password.getHash() + "'" +  
					");";
			System.out.println(insertString);
			rowsAffected = 
				conn.createStatement().executeUpdate( 
					insertString
				); 
			
		} catch (Exception e) {
			e.printStackTrace();
			
		} finally {
			if (rs != null) try { rs.close(); } catch(Exception e) {}
			if (stmt != null) try { stmt.close(); } catch(Exception e) {}
			if (conn != null) try { conn.close(); } catch(Exception e) {}
	    }
		
		return rowsAffected;	
	}
	
	@SuppressWarnings("deprecation")
	public static String toMySqlDateString(Date date) {
		StringBuilder sb = new StringBuilder();
		sb.append((date.getYear()+1900) + "-");
		sb.append((date.getMonth()+1) + "-");
		sb.append(date.getDate() + " ");
		sb.append(date.getHours()+ ":" );
		sb.append(date.getMinutes()+ ":" );
		sb.append(date.getSeconds() );
		return sb.toString();
	}
	
	
	
}
